package com.programmingteam.wallpaperslideshow;

import java.util.List;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.programmingteam.wallpaperslideshow.ui.HomeActivity;

public class PlaceSelectorView extends BaseMapActivity implements OnClickListener {
	PlacesOverlay mPlacesMapOverlay;

	private class PlacesOverlay extends ItemizedOverlay {
		private OverlayItem mOverlay;

		public PlacesOverlay(Drawable defaultMarker) {
			super(boundCenterBottom(defaultMarker));
		}

		public void addOverlay(OverlayItem overlay) {
			mOverlay = overlay;
			populate();
		}

		@Override
		protected OverlayItem createItem(int i) {
			return mOverlay;
		}

		@Override
		public int size() {
			return (mOverlay == null ? 0 : 1);
		}

		@Override
		public boolean onTap(GeoPoint geoPoint, MapView mapView) {
			addOverlay(new OverlayItem(geoPoint, "", ""));
			return true;
		}

		public GeoPoint getMarkerPosition() {
			return mOverlay.getPoint();
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mapview);
		getActivityHelper().setupActionBar("Places", 0);

		List<Overlay> mapOverlays;
		MapView mapView;
		GeoPoint geoPoint;
		OverlayItem overlayitem;
		MapController mapController;
		Drawable drawable;

		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);

		mapOverlays = mapView.getOverlays();
		drawable = this.getResources().getDrawable(R.drawable.mapmarker);
		mPlacesMapOverlay = new PlacesOverlay(drawable);

		geoPoint = getSavedPoint();
		overlayitem = new OverlayItem(geoPoint, "", "");

		mPlacesMapOverlay.addOverlay(overlayitem);
		mapOverlays.add(mPlacesMapOverlay);

		mapController = mapView.getController();
		mapController.setCenter(geoPoint);
		mapController.setZoom(8);

		((Button) findViewById(R.id.mapview_button_setselected)).setOnClickListener(this);
		((Button) findViewById(R.id.mapview_button_trackcurrent)).setOnClickListener(this);
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	private GeoPoint getSavedPoint() {
		SharedPreferences prefs;
		GeoPoint geoPoint;
		float savedLat, savedLon;
		int locationTrackType;

		prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		locationTrackType = prefs.getInt(Config.SharedKeys.LOCATION_TYPE, Config.SharedDefaults.LOCATION_TYPE);

		if (locationTrackType == Config.Const.LOCATION_TRACK_CURRENT) {
			savedLat = prefs.getFloat(Config.SharedKeys.PSEARCH_LATITUDE, Config.SharedDefaults.LOCATION_LATITUDE);
			savedLon = prefs.getFloat(Config.SharedKeys.PSEARCH_LONGITUDE, Config.SharedDefaults.LOCATION_LONGITUDE);
		}
		else { //if (locationTrackType == Config.Const.LOCATOIN_STATIC)
			savedLat = prefs.getFloat(Config.SharedKeys.LOCATION_LATITUDE, Config.SharedDefaults.LOCATION_LATITUDE);
			savedLon = prefs.getFloat(Config.SharedKeys.LOCATION_LONGITUDE, Config.SharedDefaults.LOCATION_LONGITUDE);
		}
		geoPoint = new GeoPoint((int) (savedLat * 1000000), (int) (savedLon * 1000000));
		return geoPoint;
	}

	@Override
	public void onClick(View v) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		switch (v.getId()) {
		case R.id.mapview_button_setselected:
			Editor editor;
			GeoPoint point = mPlacesMapOverlay.getMarkerPosition();
			editor = prefs.edit();
			editor.putFloat(Config.SharedKeys.LOCATION_LATITUDE, point.getLatitudeE6() / 1000000f);
			editor.putFloat(Config.SharedKeys.LOCATION_LONGITUDE, point.getLongitudeE6() / 1000000f);
			editor.putInt(Config.SharedKeys.LOCATION_TYPE, Config.Const.LOCATOIN_STATIC);
			editor.commit();
			startActivity(new Intent(this, HomeActivity.class));
			break;
		case R.id.mapview_button_trackcurrent:
			editor = prefs.edit();
			editor.putInt(Config.SharedKeys.LOCATION_TYPE, Config.Const.LOCATION_TRACK_CURRENT);
			editor.commit();
			startActivity(new Intent(this, HomeActivity.class));
			break;
		}
	}

}
